

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

public class QueryRemoteSparql {
	public static void main(String args[]) {

		ResultSet rs = null;
		long startTime = 0;
		startTime = System.currentTimeMillis();
		System.out.println("Start Time: " + startTime);
		String queryString = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who }";
		final QueryExecution qexec = QueryExecutionFactory.sparqlService(
				"http://dbpedia.org/sparql", queryString);
		try {
			rs = qexec.execSelect();
			ResultSetFormatter.outputAsRDF("RDF/XML", rs);
			int resultNumer = 0;
			for (; rs.hasNext();) {
				QuerySolution qs = rs.next();
			}
			System.out.println("Number of Results: " + resultNumer);
		} finally {
			qexec.close();
			long totalTime1 = System.currentTimeMillis() - startTime;
			System.out.println("total time: " + totalTime1);
		}
	}
}
